package com.gjun.struts.resource.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gjun.struts.bean.ext.User;
import com.gjun.struts.resource.dao.IUserDao;

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {

	


	public int saveUser(User user) {

		return (Integer)getHibernateTemplate().save(user);
	}
	
//	public static void main(String[] args) {
//		IUserDao dao = new UserDaoImpl();
//		boolean ok = dao.getUserByEmail("123213");
//		if(ok){
//			System.out.println("yyyyyyyyyyyyyyyyyyy");
//		}else{
//			System.out.println("nnnnnnnnnnnnnnnnnnn");
//		}
//	}

	public boolean getUserByEmail(String email) {
	
			String hql ="from User u where u.email=?";
			List user = getHibernateTemplate().find(hql,email);
			
		 if(user == null){
			 return true;
		 }else{
			 return false;
		 }
		
	}

	public User findUserByPwdAndEmail(String email,String pwd) {
		String hql = " from User u where u.password=? and u.email=?";
		Object[] objects = {email,pwd};
		List<User> users = getHibernateTemplate().find(hql, objects);
		User user = null;
		for (User user2 : users) {
			user = user2;
		}
		return user;
	}

	public User getUserByUserId(int userId) {
		String hql = "from User u where u.id=?";
		
		User user = (User)getHibernateTemplate().find(hql, userId).get(0);
		return user;
	}



}
